﻿// <copyright file="ConnexityCorrectKings.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains the Connexity Notifier service.</summary>

namespace Sqwarea.Batch
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using Lokad.Cloud.ServiceFabric;
    using Sqwarea.GameLogic.Connexity;
    using Sqwarea.Persistence;
    using Sqwarea.Persistence.Azure;

    /// <summary>
    /// This class acts as connexity issue notifier.
    /// </summary>
    [QueueServiceSettings(
        AutoStart = true,
        QueueName = QueueProvider.ConnexityKingToCorrectQueueName,
        ProcessingTimeoutSeconds = GlobalParameters.ConnexityCorrectKingTimeOutSeconds)]
    public class ConnexityCorrectKing : QueueService<long>
    {
        /// <summary>
        /// Gets or sets the provider of connexity
        /// </summary>
        public IConnexityProvider ConnexityProvider { get; set; }

        /// <summary>
        /// Gets or sets the game provider
        /// </summary>
        public IGameProvider GameProvider { get; set; }

        /// <summary>
        /// Entry point of the correction of king 
        /// </summary>
        /// <param name="kingId">King to correct</param>
        protected override void Start(long kingId)
        {
            IEnumerable<Square> squares = Tools.FlatArray(GameProvider.GetSquaresInRectangle(GameProvider.GetKingById(kingId).SquaresRectangle));
            Dictionary<string, Square> squaresDic = squares.ToDictionary<Square, string>(square => square.Position.ToAlphaString());
            this.ConnexityProvider.RepairsConnexity(kingId, squaresDic);
        }
    }
}
